package com.app.service.impl.user;

import com.app.domain.po.user.UserPo;
import com.app.domain.vo.user.UserVo;
import com.app.framework.utils.CoBeanUtils;
import com.app.framework.utils.CollectionUtils;
import com.app.framework.utils.StringsUtils;
import com.app.mapper.user.UserCommonMapper;
import com.app.service.user.UserCommonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户公共方法实现
 */
@Service
public class UserCommonServiceImpl extends ServiceImpl<UserCommonMapper, UserPo> implements UserCommonService {

    /**
     * 更新用户访问数量
     *
     * @param userId 用户ID
     */
    @Override
    public void updateUserLookCount(Long userId) {
        baseMapper.updateUserLookCount(userId);
    }

    /**
     * 更新用户点赞数量
     *
     * @param userId 用户ID
     */
    @Override
    public void updateUserLikeCount(Long userId) {
        baseMapper.updateUserLikeCount(userId);
    }

    /**
     * 根据OpenId查询用户
     *
     * @param openId openId
     */
    @Override
    public UserVo selectUserByOpenId(String openId) {
        return baseMapper.selectUserByOpenId(openId);
    }

    /**
     * 获取用户Map
     *
     * @param userIds 用户ID集合
     */
    @Override
    public Map<Long, UserVo> getUserMap(List<Long> userIds) {
        Map<Long, UserVo> userMap = new HashMap<>();
        if (CollectionUtils.isEmpty(userIds)) {
            return userMap;
        }
        List<Long> uniqueUserIds = StringsUtils.deduplication(userIds);
        List<UserPo> userPos = baseMapper.selectBatchIds(uniqueUserIds);
        List<UserVo> userVos = CoBeanUtils.copyList(userPos, UserVo.class);
        for (UserVo userVo : userVos) {
            userMap.put(userVo.getId(), userVo);
        }
        return userMap;
    }

}